//
//  GLViewController.m
//  GLGridView
//
//  Created by zhiyong0906 on 12/07/2017.
//  Copyright (c) 2017 zhiyong0906. All rights reserved.
//

#import "GLViewController.h"
#import <Masonry/Masonry.h>
#import "GLGridView.h"

@interface GLViewController ()
@property(nonatomic,copy)NSArray* gridDataArray;
@property(nonatomic,strong)GLGridView* gridView;
@end

@implementation GLViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"edit" style:UIBarButtonItemStylePlain target:self action:@selector(edit)];
    [self setupUI];
}

- (void)setupUI
{
    
    self.gridView.frame = self.view.bounds;
    [self.view addSubview:self.gridView];
    [self.gridView reloadData];
    
}

- (void)edit{
    _gridView.editable = !_gridView.editable;
}

- (GLGridView *)gridView
{
    if (!_gridView)
    {
        _gridView = [[GLGridView alloc]init];
        _gridView.rowCount = 4;
        _gridView.itemHeight = 90;
        
        _gridView.itemAddType = GLGridViewOperationTypeAways;
        _gridView.lineColor = [UIColor grayColor];
        _gridView.dataArray = [self.gridDataArray copy];
        [_gridView registerCell:[UICollectionViewCell class] bind:^(UICollectionViewCell*cell,NSIndexPath *indexPath,id value){
            
            UIImageView* imgV = [cell.contentView viewWithTag:88];
            if (!imgV) {
                imgV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
                imgV.tag = 88;
                imgV.center = cell.contentView.center;
                imgV.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
                [cell.contentView addSubview:imgV];
                
            }
            
            UILabel* titleLab = [cell.contentView viewWithTag:99];
            if (!titleLab) {
                titleLab = [[UILabel alloc]initWithFrame:CGRectMake(0, cell.bounds.size.height-30, cell.bounds.size.width, 30)];
                titleLab.tag = 99;
                titleLab.textAlignment = NSTextAlignmentCenter;
                titleLab.font = [UIFont systemFontOfSize:14];
                
                [cell.contentView addSubview:titleLab];
            }
            
            
            cell.contentView.backgroundColor = [UIColor whiteColor];
            NSDictionary* dict = (NSDictionary*)value;
            imgV.image = [UIImage imageNamed:dict[@"image"]];
            
            titleLab.text = dict[@"title"];
            
        }];
        
        
        _gridView.didWhenSelectItem = ^(UICollectionView*collectionView,NSIndexPath *indexPath){
            NSLog(@"indexPath%@",indexPath);
        };
        __weak typeof(self)weakSelf = self;
        _gridView.didWhenMovedItem = ^(UICollectionView *collectView, NSArray *newData) {
            weakSelf.gridDataArray = newData;
//            [[NSUserDefaults standardUserDefaults]setValue:weakSelf.gridDataArray forKey:@"grideMenuData"];
        };
    }
    return _gridView;
}

#pragma mark- setter and getter
-(NSArray*)gridDataArray{
    if (!_gridDataArray) {
        
//        _gridDataArray = [[NSUserDefaults standardUserDefaults]valueForKey:@"grideMenuData"];
        if (!_gridDataArray) {
            NSMutableArray* mData = [NSMutableArray arrayWithCapacity:0];
            
            for (NSInteger i = 0; i< 54; i++)
            {
                [mData addObject:@{@"image":@"1.jpg",@"title":[NSString stringWithFormat:@"标题%ld",i]}];
            }
            _gridDataArray = mData;
//            [[NSUserDefaults standardUserDefaults]setValue:_gridDataArray forKey:@"grideMenuData"];
        }
        
    }
    return _gridDataArray;
}


@end
